/*
 *代码由框架生成,任何更改都可能导致被代码生成器覆盖
 *Repository提供数据库操作，如果要增加数据库操作请在当前目录下Partial文件夹warehousemodelRepository编写代码
 */
using VOL.demo.IRepositories;
using VOL.Core.BaseProvider;
using VOL.Core.DbContext;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using SqlSugar;

namespace VOL.demo.Repositories
{
    public partial class warehousemodelRepository : RepositoryBase<warehousemodel>
    , IwarehousemodelRepository
    {
        public warehousemodelRepository(VOLContext dbContext)
        : base(dbContext)
        {

        }
        public static IwarehousemodelRepository Instance
        {
            get { return AutofacContainerModule.GetService<IwarehousemodelRepository>(); }
        }

        /// <summary>
        /// 根据仓库ID获取仓库信息
        /// </summary>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns>仓库信息</returns>
        public async Task<warehousemodel> GetWarehouseByIdAsync(long warehouseId)
        {
            return await DbContext.Queryable<warehousemodel>()
                .Where(x => x.Id == warehouseId && (x.IsDel == null || x.IsDel == 0))
                .FirstAsync();
        }

        /// <summary>
        /// 获取所有启用状态的仓库列表
        /// </summary>
        /// <returns>仓库列表</returns>
        public async Task<List<warehousemodel>> GetActiveWarehousesAsync()
        {
            return await DbContext.Queryable<warehousemodel>()
                .Where(x => (x.IsDel == null || x.IsDel == 0) && (x.status == 1 || x.status == null))
                .OrderBy(x => x.WarehouseName)
                .ToListAsync();
        }

        /// <summary>
        /// 根据仓库编号获取仓库信息
        /// </summary>
        /// <param name="warehouseCode">仓库编号</param>
        /// <returns>仓库信息</returns>
        public async Task<warehousemodel> GetWarehouseByCodeAsync(string warehouseCode)
        {
            if (string.IsNullOrEmpty(warehouseCode))
            {
                return null;
            }

            return await DbContext.Queryable<warehousemodel>()
                .Where(x => x.WarehouseCode == warehouseCode && (x.IsDel == null || x.IsDel == 0))
                .FirstAsync();
        }
    }
}
